/*
 * GroceriesMidlet.java
 *
 * Copyright (C) 2005 Tommi Laukkanen
 *
 * Created on May 14th 2005, 22:26
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

package substanceofcode;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;

/**
 * The Groceries Midlet is a main Midlet for this application.
 *
 * @author  Tommi Laukkanen
 * @version 1.0
 */
public class GroceriesMidlet 
        extends MIDlet 
        implements javax.microedition.lcdui.CommandListener {
    
    private Settings           m_settings;
    private GroceryListManager m_manager;
    private GroceryList        m_selectedList = null;
    
    /** Creates a new instance of GroceriesMidlet */
    public GroceriesMidlet() {
        try {
            m_settings = Settings.getInstance(this);
            m_manager = GroceryListManager.getInstance(m_settings);
        }
        catch(Exception e) {
            System.out.println("Error occured while initializing MIDlet: " + 
                    e.toString());
        }
    }

    /** Refresh the grocery list catalogue */
    private void refreshList(){
        listOfLists.deleteAll();
        Vector lists = m_manager.getAll();
        for(int i=0; i<lists.size(); i++) {
            GroceryList list = (GroceryList)lists.elementAt(i);
            listOfLists.append(list.getName(), null);
        }
    }
             
    /** Copy list's content to another list */
    private void copyListToList(List source, List destination){
        destination.deleteAll();
        for(int index=0; index<source.size(); index++) {
            destination.append(source.getString(index), null);
        }
    }    
    
    /** Refresh selected grocery list */
    private void refreshSelectedToList(){
        try {
            groceryList.deleteAll();
            groceryList.setTitle("");
            if(m_selectedList!=null) {
                groceryList.setTitle( m_selectedList.getName() );
                Vector items = m_selectedList.getItems();
                if( items != null ) {
                    for(int i=0; i<m_selectedList.getItems().size(); i++) {
                        GroceryItem item = 
                            (GroceryItem)m_selectedList.getItems().elementAt(i);
                        if( item.getDescription().length()>0 )
                            groceryList.append(item.getDescription(), null);
                            if( item.isCollected() ) {
                                groceryList.setSelectedIndex(
                                        groceryList.size()-1, true);
                            }
                    }
                }
            }
        }
        catch(Exception e) {
            System.out.println("refreshSelected error: " + e.toString());
        }
    }
    
    /** Refresh selected grocery list from list */
    private void refreshSelectedFromList(){
        try {
            
            boolean [] flags = new boolean[ groceryList.size() ];
            int selected = groceryList.getSelectedFlags( flags );

            int initialSize = groceryList.size();
            for( int index=0; index<initialSize; index++ ) {
                /** Set selected item */
                if(flags[index]==true) {
                    m_selectedList.setItemCollected( index, true );
                }
                else {
                    m_selectedList.setItemCollected( index, false );
                }
            }
        }
        catch(Exception e) {
            System.out.println("refreshSelected error: " + e.toString());
        }
    }    
    
    /** Show grocery lists */
    public void showList() {
        javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_listOfLists());
        refreshList();
    }
    
// --- This section is auto-generated by NetBeans IDE. Do not modify or you may lose your changes.//<editor-fold id="MVDMethods" defaultstate="collapsed" desc="This section is auto-generated by NetBeans IDE.">//GEN-BEGIN:MVDMethods
    /**
     * This method initializes UI of the application.
     */
    private void initialize() {
// For adding user code into this block, select "Design" item in the inspector and invoke property editor on Action property in Properties window.
        javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_splashScreen());
    }
    
    /**
     * Called by the system to indicate that a command has been invoked on a particular displayable.
     * @param command the Command that ws invoked
     * @param displayable the Displayable on which the command was invoked
     **/
    public void commandAction(javax.microedition.lcdui.Command command, javax.microedition.lcdui.Displayable displayable) {
        if (displayable == aboutForm) {
            if (command == backCommand1) {
// For adding user code into this block, select "Design | Screens | aboutForm [Form] | Assigned Commands | backCommand1" item in the inspector and invoke property editor on Action property in Properties window.
                javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_listOfLists());
            }
        } else if (displayable == addItemForm) {
            if (command == addItemCancelCommand) {
// For adding user code into this block, select "Design | Screens | addItemForm [Form] | Assigned Commands | addItemCancelCommand" item in the inspector and invoke property editor on Action property in Properties window.
                javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_groceryList());
            } else if (command == addItemOkCommand) {
// For adding user code into this block, select "Design | Screens | addItemForm [Form] | Assigned Commands | addItemOkCommand" item in the inspector and invoke property editor on Action property in Properties window.
                GroceryItem item = new GroceryItem(addItemTextField.getString(), false);
                m_selectedList.addItem( item );
                refreshSelectedToList();
                javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_groceryList());
            }
        } else if (displayable == addListForm) {
            if (command == cancelCommand) {
// For adding user code into this block, select "Design | Screens | addListForm [Form] | Assigned Commands | cancelCommand" item in the inspector and invoke property editor on Action property in Properties window.
                javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_listOfLists());
            } else if (command == okAddListCommand) {
// For adding user code into this block, select "Design | Screens | addListForm [Form] | Assigned Commands | okAddListCommand" item in the inspector and invoke property editor on Action property in Properties window.
                GroceryList list = new GroceryList(
                        listNameTextField.getString(),
                        null);
                m_manager.add( list );
                refreshList();
                javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_listOfLists());
            }
        } else if (displayable == arrangeList) {
            if (command == backCommand3) {
// For adding user code into this block, select "Design | Screens | arrangeList [Displayable] | Assigned Commands | backCommand3" item in the inspector and invoke property editor on Action property in Properties window.
                m_selectedList = arrangeList.getList();
                refreshSelectedToList();
                javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_groceryList());
            }
        } else if (displayable == groceryList) {
            if (command == PurgeSelectedCommand) {
// For adding user code into this block, select "Design | Screens | groceryList [List] | Assigned Commands | PurgeSelectedCommand" item in the inspector and invoke property editor on Action property in Properties window.
                boolean [] flags = new boolean[ groceryList.size() ];
                int selected = groceryList.getSelectedFlags( flags );
                System.out.println("List count: " + Integer.toString(selected));
                
                /** Fixer for vector array */
                int fix=0;
                int initialSize = groceryList.size();
                
                for( int index=0; index<initialSize; index++ ) {
                    /** Delete selected item */
                    if(flags[index]==true) {
                        System.out.println("Get selected: " + Integer.toString(index));
                        m_selectedList.removeItem( index-fix );
                        groceryList.delete( index-fix );
                        fix++;
                    }
                }
                
                javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_groceryList());
            } else if (command == addItemCommand) {
// For adding user code into this block, select "Design | Screens | groceryList [List] | Assigned Commands | addItemCommand" item in the inspector and invoke property editor on Action property in Properties window.
                refreshSelectedFromList();
                javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_addItemForm());
                addItemTextField.setString("");
            } else if (command == arrangeCmd) {
// For adding user code into this block, select "Design | Screens | groceryList [List] | Assigned Commands | arrangeCmd" item in the inspector and invoke property editor on Action property in Properties window.
                refreshSelectedFromList();
                m_manager.update( m_selectedList );
                javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_arrangeList());
            } else if (command == backCommand) {
// For adding user code into this block, select "Design | Screens | groceryList [List] | Assigned Commands | backCommand" item in the inspector and invoke property editor on Action property in Properties window.
                refreshSelectedFromList();
                m_manager.update( m_selectedList );
                javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_listOfLists());
            }
        } else if (displayable == listOfLists) {
            if (command == aboutCommand) {
// For adding user code into this block, select "Design | Screens | listOfLists [List] | Assigned Commands | aboutCommand" item in the inspector and invoke property editor on Action property in Properties window.
                javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_aboutForm());
            } else if (command == addListCommand) {
// For adding user code into this block, select "Design | Screens | listOfLists [List] | Assigned Commands | addListCommand" item in the inspector and invoke property editor on Action property in Properties window.
                javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_addListForm());
                listNameTextField.setString("");
            } else if (command == deleteListCommand) {
// For adding user code into this block, select "Design | Screens | listOfLists [List] | Assigned Commands | deleteListCommand" item in the inspector and invoke property editor on Action property in Properties window.
                m_selectedList = m_manager.getList(
                        listOfLists.getString( listOfLists.getSelectedIndex() ) );
                m_manager.delete( m_selectedList );
                refreshList();
                javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_listOfLists());
            } else if (command == exitCommand) {
// For adding user code into this block, select "Design | Screens | listOfLists [List] | Assigned Commands | exitCommand" item in the inspector and invoke property editor on Action property in Properties window.
                try{
                    m_manager.save();
                    m_settings.save(true);
                }catch(Exception e){
                    // Do nothing
                }
                javax.microedition.lcdui.Display.getDisplay(this).setCurrent(null);
                destroyApp(true);
                notifyDestroyed();
            } else if (command == openListCommand) {
// For adding user code into this block, select "Design | Screens | listOfLists [List] | Assigned Commands | openListCommand" item in the inspector and invoke property editor on Action property in Properties window.
                javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_groceryList());
                m_selectedList = m_manager.getList(
                        listOfLists.getString( listOfLists.getSelectedIndex() ) );
                refreshSelectedToList();
            } else if (command == renameListCommand) {
// For adding user code into this block, select "Design | Screens | listOfLists [List] | Assigned Commands | renameListCommand" item in the inspector and invoke property editor on Action property in Properties window.
                m_selectedList = m_manager.getList(
                        listOfLists.getString( listOfLists.getSelectedIndex() ) );
                javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_renameListForm());
                newNameTextField.setString( m_selectedList.getName() );
            }
        } else if (displayable == renameListForm) {
            if (command == cancelCommand1) {
// For adding user code into this block, select "Design | Screens | renameListForm [Form] | Assigned Commands | cancelCommand1" item in the inspector and invoke property editor on Action property in Properties window.
                javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_listOfLists());
            } else if (command == okRenameCommand) {
// For adding user code into this block, select "Design | Screens | renameListForm [Form] | Assigned Commands | okRenameCommand" item in the inspector and invoke property editor on Action property in Properties window.
                m_manager.delete( m_selectedList );
                m_selectedList.setName( newNameTextField.getString() );
                m_manager.add( m_selectedList );
                refreshList();
                javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_listOfLists());
            }
        } else if (displayable == splashScreen) {
            if (command == okSplashCommand) {
// For adding user code into this block, select "Design | Screens | splashScreen [Displayable] | Assigned Commands | okSplashCommand" item in the inspector and invoke property editor on Action property in Properties window.
                javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_listOfLists());
                refreshList();
            }
        }
    }
    
    /**
     * This method returns instance for listOfLists component and should be called instead of accessing listOfLists field directly.
     * @return Instance for listOfLists component
     **/
    private javax.microedition.lcdui.List get_listOfLists() {
        if (listOfLists == null) {
            listOfLists = new javax.microedition.lcdui.List("Grocery Lists", javax.microedition.lcdui.Choice.IMPLICIT, new java.lang.String[0], new javax.microedition.lcdui.Image[0]);
            listOfLists.addCommand(get_exitCommand());
            listOfLists.addCommand(get_addListCommand());
            listOfLists.addCommand(get_deleteListCommand());
            listOfLists.addCommand(get_openListCommand());
            listOfLists.addCommand(get_aboutCommand());
            listOfLists.addCommand(get_renameListCommand());
            listOfLists.setCommandListener(this);
            listOfLists.setSelectedFlags(new boolean[0]);
            listOfLists.setSelectCommand(get_openListCommand());
            listOfLists.setFitPolicy(javax.microedition.lcdui.Choice.TEXT_WRAP_ON);
        }
        return listOfLists;
    }
    
    /**
     * This method returns instance for exitCommand component and should be called instead of accessing exitCommand field directly.
     * @return Instance for exitCommand component
     **/
    private javax.microedition.lcdui.Command get_exitCommand() {
        if (exitCommand == null) {
            exitCommand = new javax.microedition.lcdui.Command("Exit", javax.microedition.lcdui.Command.EXIT, 5);
        }
        return exitCommand;
    }
    
    /**
     * This method returns instance for addListCommand component and should be called instead of accessing addListCommand field directly.
     * @return Instance for addListCommand component
     **/
    private javax.microedition.lcdui.Command get_addListCommand() {
        if (addListCommand == null) {
            addListCommand = new javax.microedition.lcdui.Command("Add List", javax.microedition.lcdui.Command.SCREEN, 2);
        }
        return addListCommand;
    }
    
    /**
     * This method returns instance for addListForm component and should be called instead of accessing addListForm field directly.
     * @return Instance for addListForm component
     **/
    private javax.microedition.lcdui.Form get_addListForm() {
        if (addListForm == null) {
            addListForm = new javax.microedition.lcdui.Form("Add List", new javax.microedition.lcdui.Item[] {get_listNameTextField()});
            addListForm.addCommand(get_okAddListCommand());
            addListForm.addCommand(get_cancelCommand());
            addListForm.setCommandListener(this);
        }
        return addListForm;
    }
    
    /**
     * This method returns instance for okAddListCommand component and should be called instead of accessing okAddListCommand field directly.
     * @return Instance for okAddListCommand component
     **/
    private javax.microedition.lcdui.Command get_okAddListCommand() {
        if (okAddListCommand == null) {
            okAddListCommand = new javax.microedition.lcdui.Command("Ok", javax.microedition.lcdui.Command.OK, 1);
        }
        return okAddListCommand;
    }
    
    /**
     * This method returns instance for cancelCommand component and should be called instead of accessing cancelCommand field directly.
     * @return Instance for cancelCommand component
     **/
    private javax.microedition.lcdui.Command get_cancelCommand() {
        if (cancelCommand == null) {
            cancelCommand = new javax.microedition.lcdui.Command("Cancel", javax.microedition.lcdui.Command.CANCEL, 2);
        }
        return cancelCommand;
    }
    
    /**
     * This method returns instance for deleteListCommand component and should be called instead of accessing deleteListCommand field directly.
     * @return Instance for deleteListCommand component
     **/
    private javax.microedition.lcdui.Command get_deleteListCommand() {
        if (deleteListCommand == null) {
            deleteListCommand = new javax.microedition.lcdui.Command("Delete List", javax.microedition.lcdui.Command.ITEM, 3);
        }
        return deleteListCommand;
    }
    
    /**
     * This method returns instance for openListCommand component and should be called instead of accessing openListCommand field directly.
     * @return Instance for openListCommand component
     **/
    private javax.microedition.lcdui.Command get_openListCommand() {
        if (openListCommand == null) {
            openListCommand = new javax.microedition.lcdui.Command("Open List", javax.microedition.lcdui.Command.SCREEN, 1);
        }
        return openListCommand;
    }
    
    /**
     * This method returns instance for groceryList component and should be called instead of accessing groceryList field directly.
     * @return Instance for groceryList component
     **/
    private javax.microedition.lcdui.List get_groceryList() {
        if (groceryList == null) {
            groceryList = new javax.microedition.lcdui.List("Groceries", javax.microedition.lcdui.Choice.MULTIPLE, new java.lang.String[0], new javax.microedition.lcdui.Image[0]);
            groceryList.addCommand(get_addItemCommand());
            groceryList.addCommand(get_backCommand());
            groceryList.addCommand(get_PurgeSelectedCommand());
            groceryList.addCommand(get_arrangeCmd());
            groceryList.setCommandListener(this);
            groceryList.setSelectedFlags(new boolean[0]);
            groceryList.setSelectCommand(get_selectItem());
        }
        return groceryList;
    }
    
    /**
     * This method returns instance for addItemCommand component and should be called instead of accessing addItemCommand field directly.
     * @return Instance for addItemCommand component
     **/
    private javax.microedition.lcdui.Command get_addItemCommand() {
        if (addItemCommand == null) {
            addItemCommand = new javax.microedition.lcdui.Command("Add Item", javax.microedition.lcdui.Command.SCREEN, 2);
        }
        return addItemCommand;
    }
    
    /**
     * This method returns instance for backCommand component and should be called instead of accessing backCommand field directly.
     * @return Instance for backCommand component
     **/
    private javax.microedition.lcdui.Command get_backCommand() {
        if (backCommand == null) {
            backCommand = new javax.microedition.lcdui.Command("Back", javax.microedition.lcdui.Command.BACK, 1);
        }
        return backCommand;
    }
    
    /**
     * This method returns instance for backEditCommand component and should be called instead of accessing backEditCommand field directly.
     * @return Instance for backEditCommand component
     **/
    private javax.microedition.lcdui.Command get_backEditCommand() {
        if (backEditCommand == null) {
            backEditCommand = new javax.microedition.lcdui.Command("Back", javax.microedition.lcdui.Command.BACK, 1);
        }
        return backEditCommand;
    }
    
    /**
     * This method returns instance for listNameTextField component and should be called instead of accessing listNameTextField field directly.
     * @return Instance for listNameTextField component
     **/
    private javax.microedition.lcdui.TextField get_listNameTextField() {
        if (listNameTextField == null) {
            listNameTextField = new javax.microedition.lcdui.TextField("Grocery List Name", null, 120, 0x0);
        }
        return listNameTextField;
    }
    
    /**
     * This method returns instance for aboutForm component and should be called instead of accessing aboutForm field directly.
     * @return Instance for aboutForm component
     **/
    private javax.microedition.lcdui.Form get_aboutForm() {
        if (aboutForm == null) {
            aboutForm = new javax.microedition.lcdui.Form("About", new javax.microedition.lcdui.Item[] {
                get_stringItem1(),
                        get_spacer1(),
                        get_stringItem3(),
                        get_stringItem2()
            });
            aboutForm.addCommand(get_backCommand1());
            aboutForm.setCommandListener(this);
        }
        return aboutForm;
    }
    
    /**
     * This method returns instance for aboutCommand component and should be called instead of accessing aboutCommand field directly.
     * @return Instance for aboutCommand component
     **/
    private javax.microedition.lcdui.Command get_aboutCommand() {
        if (aboutCommand == null) {
            aboutCommand = new javax.microedition.lcdui.Command("About", javax.microedition.lcdui.Command.SCREEN, 4);
        }
        return aboutCommand;
    }
    
    /**
     * This method returns instance for stringItem1 component and should be called instead of accessing stringItem1 field directly.
     * @return Instance for stringItem1 component
     **/
    private javax.microedition.lcdui.StringItem get_stringItem1() {
        if (stringItem1 == null) {
            stringItem1 = new javax.microedition.lcdui.StringItem("Groceries v1.1 ", "Copyright (C) 2005\nTommi Laukkanen\nwww.substanceofcode.com");
        }
        return stringItem1;
    }
    
    /**
     * This method returns instance for backCommand1 component and should be called instead of accessing backCommand1 field directly.
     * @return Instance for backCommand1 component
     **/
    private javax.microedition.lcdui.Command get_backCommand1() {
        if (backCommand1 == null) {
            backCommand1 = new javax.microedition.lcdui.Command("Back", javax.microedition.lcdui.Command.BACK, 1);
        }
        return backCommand1;
    }
    
    /**
     * This method returns instance for backEditCommand1 component and should be called instead of accessing backEditCommand1 field directly.
     * @return Instance for backEditCommand1 component
     **/
    private javax.microedition.lcdui.Command get_backEditCommand1() {
        if (backEditCommand1 == null) {
            backEditCommand1 = new javax.microedition.lcdui.Command("Back", javax.microedition.lcdui.Command.BACK, 1);
        }
        return backEditCommand1;
    }
    
    /**
     * This method returns instance for nextItemCommand component and should be called instead of accessing nextItemCommand field directly.
     * @return Instance for nextItemCommand component
     **/
    private javax.microedition.lcdui.Command get_nextItemCommand() {
        if (nextItemCommand == null) {
            nextItemCommand = new javax.microedition.lcdui.Command("Next", javax.microedition.lcdui.Command.ITEM, 1);
        }
        return nextItemCommand;
    }
    
    /**
     * This method returns instance for renameListForm component and should be called instead of accessing renameListForm field directly.
     * @return Instance for renameListForm component
     **/
    private javax.microedition.lcdui.Form get_renameListForm() {
        if (renameListForm == null) {
            renameListForm = new javax.microedition.lcdui.Form("Rename List", new javax.microedition.lcdui.Item[] {get_newNameTextField()});
            renameListForm.addCommand(get_okRenameCommand());
            renameListForm.addCommand(get_cancelCommand1());
            renameListForm.setCommandListener(this);
        }
        return renameListForm;
    }
    
    /**
     * This method returns instance for okRenameCommand component and should be called instead of accessing okRenameCommand field directly.
     * @return Instance for okRenameCommand component
     **/
    private javax.microedition.lcdui.Command get_okRenameCommand() {
        if (okRenameCommand == null) {
            okRenameCommand = new javax.microedition.lcdui.Command("Ok", javax.microedition.lcdui.Command.OK, 1);
        }
        return okRenameCommand;
    }
    
    /**
     * This method returns instance for exitCommand1 component and should be called instead of accessing exitCommand1 field directly.
     * @return Instance for exitCommand1 component
     **/
    private javax.microedition.lcdui.Command get_exitCommand1() {
        if (exitCommand1 == null) {
            exitCommand1 = new javax.microedition.lcdui.Command("Exit", javax.microedition.lcdui.Command.EXIT, 1);
        }
        return exitCommand1;
    }
    
    /**
     * This method returns instance for cancelCommand1 component and should be called instead of accessing cancelCommand1 field directly.
     * @return Instance for cancelCommand1 component
     **/
    private javax.microedition.lcdui.Command get_cancelCommand1() {
        if (cancelCommand1 == null) {
            cancelCommand1 = new javax.microedition.lcdui.Command("Cancel", javax.microedition.lcdui.Command.CANCEL, 1);
        }
        return cancelCommand1;
    }
    
    /**
     * This method returns instance for renameListCommand component and should be called instead of accessing renameListCommand field directly.
     * @return Instance for renameListCommand component
     **/
    private javax.microedition.lcdui.Command get_renameListCommand() {
        if (renameListCommand == null) {
            renameListCommand = new javax.microedition.lcdui.Command("Rename List", javax.microedition.lcdui.Command.SCREEN, 1);
        }
        return renameListCommand;
    }
    
    /**
     * This method returns instance for newNameTextField component and should be called instead of accessing newNameTextField field directly.
     * @return Instance for newNameTextField component
     **/
    private javax.microedition.lcdui.TextField get_newNameTextField() {
        if (newNameTextField == null) {
            newNameTextField = new javax.microedition.lcdui.TextField("Enter new title", null, 120, 0x0);
        }
        return newNameTextField;
    }
    
    /**
     * This method returns instance for selectItem component and should be called instead of accessing selectItem field directly.
     * @return Instance for selectItem component
     **/
    private javax.microedition.lcdui.Command get_selectItem() {
        if (selectItem == null) {
            selectItem = new javax.microedition.lcdui.Command("Select Item", javax.microedition.lcdui.Command.ITEM, 1);
        }
        return selectItem;
    }
    
    /**
     * This method returns instance for addItemForm component and should be called instead of accessing addItemForm field directly.
     * @return Instance for addItemForm component
     **/
    private javax.microedition.lcdui.Form get_addItemForm() {
        if (addItemForm == null) {
            addItemForm = new javax.microedition.lcdui.Form("Add Item", new javax.microedition.lcdui.Item[] {get_addItemTextField()});
            addItemForm.addCommand(get_addItemOkCommand());
            addItemForm.addCommand(get_addItemCancelCommand());
            addItemForm.setCommandListener(this);
        }
        return addItemForm;
    }
    
    /**
     * This method returns instance for addItemOkCommand component and should be called instead of accessing addItemOkCommand field directly.
     * @return Instance for addItemOkCommand component
     **/
    private javax.microedition.lcdui.Command get_addItemOkCommand() {
        if (addItemOkCommand == null) {
            addItemOkCommand = new javax.microedition.lcdui.Command("Ok", javax.microedition.lcdui.Command.OK, 1);
        }
        return addItemOkCommand;
    }
    
    /**
     * This method returns instance for addItemCancelCommand component and should be called instead of accessing addItemCancelCommand field directly.
     * @return Instance for addItemCancelCommand component
     **/
    private javax.microedition.lcdui.Command get_addItemCancelCommand() {
        if (addItemCancelCommand == null) {
            addItemCancelCommand = new javax.microedition.lcdui.Command("Cancel", javax.microedition.lcdui.Command.CANCEL, 1);
        }
        return addItemCancelCommand;
    }
    
    /**
     * This method returns instance for addItemTextField component and should be called instead of accessing addItemTextField field directly.
     * @return Instance for addItemTextField component
     **/
    private javax.microedition.lcdui.TextField get_addItemTextField() {
        if (addItemTextField == null) {
            addItemTextField = new javax.microedition.lcdui.TextField("Item description", null, 120, 0x0);
        }
        return addItemTextField;
    }
    
    /**
     * This method returns instance for PurgeSelectedCommand component and should be called instead of accessing PurgeSelectedCommand field directly.
     * @return Instance for PurgeSelectedCommand component
     **/
    private javax.microedition.lcdui.Command get_PurgeSelectedCommand() {
        if (PurgeSelectedCommand == null) {
            PurgeSelectedCommand = new javax.microedition.lcdui.Command("Purge Selected", javax.microedition.lcdui.Command.ITEM, 4);
        }
        return PurgeSelectedCommand;
    }
    
    /**
     * This method returns instance for selectCommand component and should be called instead of accessing selectCommand field directly.
     * @return Instance for selectCommand component
     **/
    private javax.microedition.lcdui.Command get_selectCommand() {
        if (selectCommand == null) {
            selectCommand = new javax.microedition.lcdui.Command("Select", javax.microedition.lcdui.Command.ITEM, 1);
        }
        return selectCommand;
    }
    
    /**
     * This method returns instance for spacer1 component and should be called instead of accessing spacer1 field directly.
     * @return Instance for spacer1 component
     **/
    private javax.microedition.lcdui.Spacer get_spacer1() {
        if (spacer1 == null) {
            spacer1 = new javax.microedition.lcdui.Spacer(1000, 1);
        }
        return spacer1;
    }
    
    /**
     * This method returns instance for stringItem2 component and should be called instead of accessing stringItem2 field directly.
     * @return Instance for stringItem2 component
     **/
    private javax.microedition.lcdui.StringItem get_stringItem2() {
        if (stringItem2 == null) {
            stringItem2 = new javax.microedition.lcdui.StringItem("License\n", "GPL (See http://www.gnu.org/copyleft/gpl.html for more information)");
        }
        return stringItem2;
    }
    
    /**
     * This method returns instance for stringItem3 component and should be called instead of accessing stringItem3 field directly.
     * @return Instance for stringItem3 component
     **/
    private javax.microedition.lcdui.StringItem get_stringItem3() {
        if (stringItem3 == null) {
            stringItem3 = new javax.microedition.lcdui.StringItem("Donations\n", "If you like this application, please make a donation via PayPal. See more information on http://www.substanceofcode.com");
        }
        return stringItem3;
    }
    
    /**
     * This method returns instance for okSplashCommand1 component and should be called instead of accessing okSplashCommand1 field directly.
     * @return Instance for okSplashCommand1 component
     **/
    private javax.microedition.lcdui.Command get_okSplashCommand1() {
        if (okSplashCommand1 == null) {
            okSplashCommand1 = new javax.microedition.lcdui.Command("Ok", javax.microedition.lcdui.Command.OK, 1);
        }
        return okSplashCommand1;
    }
    
    /**
     * This method returns instance for okCommand1 component and should be called instead of accessing okCommand1 field directly.
     * @return Instance for okCommand1 component
     **/
    private javax.microedition.lcdui.Command get_okCommand1() {
        if (okCommand1 == null) {
            okCommand1 = new javax.microedition.lcdui.Command("Ok", javax.microedition.lcdui.Command.OK, 1);
        }
        return okCommand1;
    }
    
    /**
     * This method returns instance for splashScreen component and should be called instead of accessing splashScreen field directly.
     * @return Instance for splashScreen component
     **/
    private substanceofcode.SplashCanvas get_splashScreen() {
        if (splashScreen == null) {
            splashScreen = new substanceofcode.SplashCanvas(this);
            splashScreen.addCommand(get_okSplashCommand());
            splashScreen.setCommandListener(this);
        }
        return splashScreen;
    }
    
    /**
     * This method returns instance for okSplashCommand component and should be called instead of accessing okSplashCommand field directly.
     * @return Instance for okSplashCommand component
     **/
    private javax.microedition.lcdui.Command get_okSplashCommand() {
        if (okSplashCommand == null) {
            okSplashCommand = new javax.microedition.lcdui.Command("Ok", javax.microedition.lcdui.Command.OK, 1);
        }
        return okSplashCommand;
    }
    
    /**
     * This method returns instance for manageListCmd component and should be called instead of accessing manageListCmd field directly.
     * @return Instance for manageListCmd component
     **/
    private javax.microedition.lcdui.Command get_manageListCmd() {
        if (manageListCmd == null) {
            manageListCmd = new javax.microedition.lcdui.Command("Manage List", javax.microedition.lcdui.Command.SCREEN, 1);
        }
        return manageListCmd;
    }
    
    /**
     * This method returns instance for backCommand2 component and should be called instead of accessing backCommand2 field directly.
     * @return Instance for backCommand2 component
     **/
    private javax.microedition.lcdui.Command get_backCommand2() {
        if (backCommand2 == null) {
            backCommand2 = new javax.microedition.lcdui.Command("Back", javax.microedition.lcdui.Command.BACK, 1);
        }
        return backCommand2;
    }
    
    /**
     * This method returns instance for moveItemCmd component and should be called instead of accessing moveItemCmd field directly.
     * @return Instance for moveItemCmd component
     **/
    private javax.microedition.lcdui.Command get_moveItemCmd() {
        if (moveItemCmd == null) {
            moveItemCmd = new javax.microedition.lcdui.Command("Move up", javax.microedition.lcdui.Command.ITEM, 1);
        }
        return moveItemCmd;
    }
    
    /**
     * This method returns instance for moveItemDownCmd component and should be called instead of accessing moveItemDownCmd field directly.
     * @return Instance for moveItemDownCmd component
     **/
    private javax.microedition.lcdui.Command get_moveItemDownCmd() {
        if (moveItemDownCmd == null) {
            moveItemDownCmd = new javax.microedition.lcdui.Command("Move down", "null", javax.microedition.lcdui.Command.ITEM, 1);
        }
        return moveItemDownCmd;
    }
    
    /**
     * This method returns instance for arrangeList component and should be called instead of accessing arrangeList field directly.
     * @return Instance for arrangeList component
     **/
    private substanceofcode.groceries.ui.EditableList get_arrangeList() {
        if (arrangeList == null) {
            arrangeList = new substanceofcode.groceries.ui.EditableList(this, m_selectedList);
            arrangeList.addCommand(get_backCommand3());
            arrangeList.setCommandListener(this);
            arrangeList.setTitle("Arrange List");
        }
        return arrangeList;
    }
    
    /**
     * This method returns instance for backCommand3 component and should be called instead of accessing backCommand3 field directly.
     * @return Instance for backCommand3 component
     **/
    private javax.microedition.lcdui.Command get_backCommand3() {
        if (backCommand3 == null) {
            backCommand3 = new javax.microedition.lcdui.Command("Back", javax.microedition.lcdui.Command.BACK, 1);
        }
        return backCommand3;
    }
    
    /**
     * This method returns instance for arrangeCmd component and should be called instead of accessing arrangeCmd field directly.
     * @return Instance for arrangeCmd component
     **/
    private javax.microedition.lcdui.Command get_arrangeCmd() {
        if (arrangeCmd == null) {
            arrangeCmd = new javax.microedition.lcdui.Command("Arrange List", javax.microedition.lcdui.Command.SCREEN, 3);
        }
        return arrangeCmd;
    }
    
    javax.microedition.lcdui.List listOfLists;
    javax.microedition.lcdui.Command exitCommand;
    javax.microedition.lcdui.Command addListCommand;
    javax.microedition.lcdui.Form addListForm;
    javax.microedition.lcdui.Command okAddListCommand;
    javax.microedition.lcdui.Command cancelCommand;
    javax.microedition.lcdui.Command deleteListCommand;
    javax.microedition.lcdui.Command openListCommand;
    javax.microedition.lcdui.List groceryList;
    javax.microedition.lcdui.Command addItemCommand;
    javax.microedition.lcdui.Command backCommand;
    javax.microedition.lcdui.Command backEditCommand;
    javax.microedition.lcdui.TextField listNameTextField;
    javax.microedition.lcdui.Form aboutForm;
    javax.microedition.lcdui.Command aboutCommand;
    javax.microedition.lcdui.StringItem stringItem1;
    javax.microedition.lcdui.Command backCommand1;
    javax.microedition.lcdui.Command backEditCommand1;
    javax.microedition.lcdui.Command nextItemCommand;
    javax.microedition.lcdui.Form renameListForm;
    javax.microedition.lcdui.Command okRenameCommand;
    javax.microedition.lcdui.Command exitCommand1;
    javax.microedition.lcdui.Command cancelCommand1;
    javax.microedition.lcdui.Command renameListCommand;
    javax.microedition.lcdui.TextField newNameTextField;
    javax.microedition.lcdui.Command selectItem;
    javax.microedition.lcdui.Form addItemForm;
    javax.microedition.lcdui.Command addItemOkCommand;
    javax.microedition.lcdui.Command addItemCancelCommand;
    javax.microedition.lcdui.TextField addItemTextField;
    javax.microedition.lcdui.Command PurgeSelectedCommand;
    javax.microedition.lcdui.Command selectCommand;
    javax.microedition.lcdui.Spacer spacer1;
    javax.microedition.lcdui.StringItem stringItem2;
    javax.microedition.lcdui.StringItem stringItem3;
    javax.microedition.lcdui.Command okSplashCommand1;
    javax.microedition.lcdui.Command okCommand1;
    substanceofcode.SplashCanvas splashScreen;
    javax.microedition.lcdui.Command okSplashCommand;
    javax.microedition.lcdui.Command manageListCmd;
    javax.microedition.lcdui.Command backCommand2;
    javax.microedition.lcdui.Command moveItemCmd;
    javax.microedition.lcdui.Command moveItemDownCmd;
    substanceofcode.groceries.ui.EditableList arrangeList;
    javax.microedition.lcdui.Command backCommand3;
    javax.microedition.lcdui.Command arrangeCmd;
// --- This is the end of auto-generated section.//</editor-fold>//GEN-END:MVDMethods
    
    public void startApp() {
        initialize();
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
    }
    
}
